package csel.mapmaker;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectOutputStream;

import csel.model.*;
import csel.model.areaeffect.*;
import csel.model.entity.Smasher;
import csel.model.entity.Tait;
import csel.model.entity.WheelyChair;
import csel.model.items.*;

public class MapMaker
{
	/**
	 * @param args
	 */

	/*
	 * as of now makes this really simple and ugly map: (X is wall, o is desk,
	 * space is floor)
	 * 
	 * XXXXXXXX X X Xo oo oX Xo oX Xo oo oX Xo oX XooooooX XXXXXXXX
	 * 
	 */
	private static RealTile[][] tileMatrix;
	
	protected static Direction FLOOR_GRAY = Direction.NORTH;
	protected static Direction FLOOR_CARPET = Direction.SOUTHWEST;
	protected static Direction FLOOR_YELLOW = Direction.WEST;
	protected static Direction FLOOR_BROWN = Direction.NORTHWEST;
	protected static Direction WALL_BRICK = Direction.NORTH;
	protected static Direction WALL_WHITE = Direction.SOUTHWEST;
	
	protected static Direction DESK_TOILETDOWN = Direction.SOUTHWEST;
	protected static Direction DESK_TOILETUP = Direction.WEST;
	protected static Direction DESK_DESKDOWN = Direction.NORTH;
	protected static Direction DESK_DESKUP = Direction.NORTHWEST;
	protected static Direction DESK_DESKLEFT = Direction.NORTHEAST;
	protected static Direction DESK_DESKRIGHT = Direction.EAST;
	protected static Direction DESK_TABLE = Direction.SOUTH;

	protected static Floor makeFloor(Direction d)
	{
		Floor f = new Floor("floor");
		f.setDirection(d);
		return f;
	}
	
	protected static Wall makeWall(Direction d)
	{
		Wall w = new Wall("wall");
		w.setDirection(d);
		return w;
	}
	
	protected static Desk makeDesk(Direction d)
	{
		Desk desk = new Desk("desk");
		desk.setDirection(d);
		return desk;
	}

	public static void main(String[] args) throws IOException
	{
		System.out.println("Working Directory = "
				+ System.getProperty("user.dir"));

		int width = 139;
		int height = 39;
		tileMatrix = new RealTile[width][height];
		File f = new File("csebasement.txt");
		if (f.exists())
			System.out.println("I found the file!");

		Map m = new Map(tileMatrix, 4, 2);

		char oneChar;
		BufferedReader input = new BufferedReader(new FileReader(f));
		int a = 0, b = 0;
		while (((oneChar = ((char) input.read())) != '\0') && (b < height))
		{
			RealTile t = new RealTile(m);
			if (oneChar == 'X')
			{
				t.setTerrain(makeWall(WALL_BRICK));
				t.setLocation(new Location(a, b));
				tileMatrix[a][b] = t;
				System.out.print("w");
				++a;
			} 
			else if (oneChar == 'Q')
			{
				t.setTerrain(makeWall(WALL_WHITE));
				t.setLocation(new Location(a, b));
				tileMatrix[a][b] = t;
				System.out.print("Q");
				++a;
			}
			else if (oneChar == 'o')
			{
				t.setTerrain(makeDesk(DESK_TABLE));
				t.setLocation(new Location(a, b));
				tileMatrix[a][b] = t;
				System.out.print("o");
				++a;
			}
			else if (oneChar == '{')
			{
				t.setTerrain(makeDesk(DESK_TOILETDOWN));
				t.setLocation(new Location(a, b));
				tileMatrix[a][b] = t;
				System.out.print("o");
				++a;
			}
			else if (oneChar == '}')
			{
				t.setTerrain(makeDesk(DESK_TOILETUP));
				t.setLocation(new Location(a, b));
				tileMatrix[a][b] = t;
				System.out.print("o");
				++a;
			}
			else if (oneChar == '[')
			{
				t.setTerrain(makeDesk(DESK_DESKDOWN));
				t.setLocation(new Location(a, b));
				tileMatrix[a][b] = t;
				System.out.print("o");
				++a;
			}
			else if (oneChar == ']')
			{
				t.setTerrain(makeDesk(DESK_DESKUP));
				t.setLocation(new Location(a, b));
				tileMatrix[a][b] = t;
				System.out.print("o");
				++a;
			}
			else if (oneChar == '(')
			{
				t.setTerrain(makeDesk(DESK_DESKLEFT));
				t.setLocation(new Location(a, b));
				tileMatrix[a][b] = t;
				System.out.print("o");
				++a;
			}
			else if (oneChar == ')')
			{
				t.setTerrain(makeDesk(DESK_DESKRIGHT));
				t.setLocation(new Location(a, b));
				tileMatrix[a][b] = t;
				System.out.print("o");
				++a;
			}
			else if (oneChar == 'p')
			{
				t.setTerrain(makeFloor(FLOOR_YELLOW));
				t.setLocation(new Location(a, b));
				tileMatrix[a][b] = t;
				System.out.print("p");
				++a;
			} else if (oneChar == 'c')
			{
				t.setTerrain(makeFloor(FLOOR_CARPET));
				t.setLocation(new Location(a, b));
				tileMatrix[a][b] = t;
				System.out.print("p");
				++a;
			} else if (oneChar == ' ')
			{
				t.setTerrain(makeFloor(FLOOR_GRAY));
				t.setLocation(new Location(a, b));
				tileMatrix[a][b] = t;
				System.out.print(" ");
				++a;
			} else if (oneChar == 'n')
			{
				t = null;
				tileMatrix[a][b] = t;
				System.out.print("n");
				++a;
			} else if (oneChar == 'b')
			{
				t.setTerrain(makeFloor(FLOOR_BROWN));
				t.setLocation(new Location(a, b));
				tileMatrix[a][b] = t;
				System.out.print(" ");
				++a;
			}

			if (a >= width)
			{
				a = 0;
				++b;
				System.out.println();
			}
		}

		tileMatrix[42][15].setAreaEffect(new InstantDeath());
		tileMatrix[42][15].setDecal(new Decal("skull"));
		tileMatrix[33][1].setAreaEffect(new LevelUp());
		tileMatrix[33][1].setDecal(new Decal("star"));
		tileMatrix[38][7].setAreaEffect(new HurtPlayer(8));
		tileMatrix[38][7].setDecal(new Decal("redx"));
		tileMatrix[19][22].setAreaEffect(new HurtPlayer(8));
		tileMatrix[19][22].setDecal(new Decal("redx"));
		tileMatrix[20][22].setAreaEffect(new HurtPlayer(8));
		tileMatrix[20][22].setDecal(new Decal("redx"));
		tileMatrix[3][22].setAreaEffect(new HealDamage(8));
		tileMatrix[3][22].setDecal(new Decal("gcross"));
		tileMatrix[12][32].setAreaEffect(new Teleporter(tileMatrix[59][3]));
		tileMatrix[59][1].setAreaEffect(new Teleporter(tileMatrix[15][32]));
		tileMatrix[2][22].setAreaEffect(new Teleporter(tileMatrix[82][26]));
		Decal stairs1 = new Decal("stairs");
		stairs1.setDirection(Direction.SOUTHWEST);
		tileMatrix[2][22].setDecal(stairs1);
		tileMatrix[83][26].setAreaEffect(new Teleporter(tileMatrix[2][21]));
		Decal stairs2 = new Decal("stairs");
		stairs2.setDirection(Direction.WEST);
		tileMatrix[83][26].setDecal(stairs2);
		Decal warp = new Decal("warp");
		tileMatrix[59][1].setDecal(warp);

		MediumHealthPotion p = new MediumHealthPotion();
		tileMatrix[17][18].setItem(p);

		WheelyChair wc = new WheelyChair("chair1");
		wc.move(tileMatrix[18][19]);
		
		PencilOfStabbing w = new PencilOfStabbing();
		tileMatrix[42][8].setItem(w);
		tileMatrix[42][8].setDecal(new Decal("puddle"));

		Shield shield = new ImpenetrableBackpack();
		tileMatrix[7][36].setItem(shield);
		
		// TODO green linen shirt
		
		BodyArmor bodyArmor = new WhiteTee();
		tileMatrix[59][2].setItem(bodyArmor);
		
//		DamageItem d = new DamageItem(13);
//		d.setLocation(tileMatrix[14][6]);
//		tileMatrix[14][6].setItem(d);
//
		GatorOne k = new GatorOne();
		k.setDescription("Key to The City");
		k.setLocation(tileMatrix[7][2]);
		tileMatrix[7][2].setItem(k);

		GatorOneLockableDoor door1 = new GatorOneLockableDoor();
		door1.setDirection(Direction.SOUTH);
		door1.setLocation(tileMatrix[9][3]);
		tileMatrix[9][3].setItem(door1);

		GatorOneLockableDoor door2 = new GatorOneLockableDoor();
		door2.setDirection(Direction.WEST);
		door2.setLocation(tileMatrix[15][14]);
		tileMatrix[15][14].setItem(door2);

		GatorOneLockableDoor door3 = new GatorOneLockableDoor();
		door3.setDirection(Direction.EAST);
		door3.setLocation(tileMatrix[35][14]);
		tileMatrix[35][14].setItem(door3);

		GatorOneLockableDoor door4 = new GatorOneLockableDoor();
		door4.setDirection(Direction.SOUTH);
		door4.setLocation(tileMatrix[29][20]);
		tileMatrix[29][20].setItem(door4);

		ObstacleItem ob = new ObstacleItem("wetfloor");
		tileMatrix[13][6].setItem(ob);

		ObstacleItem ob1 = new ObstacleItem("wetfloor");
		tileMatrix[42][6].setItem(ob1);

		ObstacleItem ob2 = new ObstacleItem("wetfloor");
		tileMatrix[8][19].setItem(ob2);

		ObstacleItem ob3 = new ObstacleItem("wetfloor");
		tileMatrix[7][25].setItem(ob3);

		Tait tait = new Tait(new Smasher());
		tait.init();
		tait.setDirection(Direction.WEST);
		//tileMatrix[22][19].setEntity(tait);
		tait.move(tileMatrix[22][19]);
		
		KillerTrap kt = new KillerTrap();
		tileMatrix[26][19].setItem(kt);
		
		printThingie();
		try
		{
			mapSaver(m);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public static void mapSaver(Map m) throws FileNotFoundException,
			IOException
	{
		FileOutputStream fileOut = new FileOutputStream("../csel/maps/default.map");
		ObjectOutputStream serializer = new ObjectOutputStream(fileOut);

		serializer.writeObject(m);

		serializer.close();
		fileOut.close();
	}

	public static void printThingie()
	{
		int size1 = 39;
		int size2 = 139;
		for (int b = 0; b < size1; ++b)
		{
			for (int a = 0; a < size2; ++a)
			{
				if (tileMatrix[a][b] == null)
					System.out.print("n");
				else if (tileMatrix[a][b].getTerrain() instanceof Wall)
					System.out.print("X");
				else if (tileMatrix[a][b].getTerrain() instanceof Desk)
					System.out.print("o");
				else if (tileMatrix[a][b].getTerrain() instanceof Floor)
					System.out.print(" ");
				else
					System.out.print("THIS AINT RIGHT!");
			}
			System.out.println();
		}
	}

}
